package com.jlkf.service.util;

import java.util.HashMap;
import java.util.Map;

import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.SMS;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.Notification;

public class AuroraPush {

	public static String secret = "15f21adec827265f0371aec2";//司机端
	public static String appkey = "afc5984527d4b9900295e993";
	
	public static String secret1 = "192fd30ad234a9a8e09416b6";//用户端  93de37e3639830a92f38aa7e   极光用户端
	public static String appkey1 = "93de37e3639830a92f38aa7e";//用户端  93de37e3639830a92f38aa7e   极光用户端
	
	/*public static String secret = "de73bd5c8eb89b04ab165728";
	public static String appkey = "099a69c4e23a36a63ff7145a";*/
	
	public static void pushAll(String msg){
		JPushClient jpushClient = new JPushClient(secret, appkey, null, ClientConfig.getInstance());

	    // For push, all you need do is to build PushPayload object.
	    PushPayload payload = buildPushObject_all_all_alert(msg);

	    try {
	        PushResult result = jpushClient.sendPush(payload);
	        System.out.println("pushAll="+result);
//	        LOG.info("Got result - " + result);
	    } catch (APIConnectionException e) {
	    	e.printStackTrace();
//	        LOG.error("Connection error, should retry later", e);
	    } catch (APIRequestException e) {
	        // Should review the error, and fix the request
	        //LOG.error("Should review the error, and fix the request", e);
	        //LOG.info("HTTP Status: " + e.getStatus());
	        //LOG.info("Error Code: " + e.getErrorCode());
	        //LOG.info("Error Message: " + e.getErrorMessage());
	    	e.printStackTrace();
	    }
	}
	//aurora_alias  使用平台
	public static void pushTargetForDriver(String where,String msg,String aurora_alias){
		JPushClient jpushClient = new JPushClient(secret, appkey, null, ClientConfig.getInstance());
       System.out.println(msg);
       
   			try {
   				if("ios".equals(aurora_alias)){
   		    		//PushResult result1 = jpushClient.sendIosMessageWithAlias(title, msgContent, alias);
   		    		PushPayload pp=PushPayload.newBuilder()
   		    				.setPlatform(Platform.ios())
   		    				.setMessage(Message.newBuilder()
   	                        .setMsgContent(msg).build())
   		    				.setAudience(Audience.alias(where))
   		    		.build();
   		    		PushResult result = jpushClient.sendPush(pp);
   			        System.out.println("ios消息="+result);
   		    	}else{
//   		    		PushResult result = jpushClient.sendAndroidMessageWithAlias(title, msgContent, alias);
   		    		PushPayload pp=PushPayload.newBuilder()
   		    				.setPlatform(Platform.android())
   		    				.setMessage(Message.newBuilder()
   	                        .setMsgContent(msg).build())
   		    				.setAudience(Audience.alias(where))
   		    		.build();
   						PushResult result = jpushClient.sendPush(pp);
   			        System.out.println("Android消息="+result);
   		    	}
   			} catch (APIConnectionException e) {
   				// TODO Auto-generated catch block
   				e.printStackTrace();
   			} catch (APIRequestException e) {
   				// TODO Auto-generated catch block
   				e.printStackTrace();
   			}
       
	    // For push, all you need do is to build PushPayload object.
	   /* PushPayload payload = buildPushObject_all_alias_alert(where,msg);

	    try {
	        PushResult result = jpushClient.sendPush(payload);
	        System.out.println(result);*/
	        //LOG.info("Got result - " + result);
	   /* } catch (APIConnectionException e) {
	    	e.printStackTrace();*/
	    	// Connection error, should retry later
	        //LOG.error("Connection error, should retry later", e);
	   /* } catch (APIRequestException e) {
	    	e.printStackTrace();*/
	    	// Should review the error, and fix the request
	        //LOG.error("Should review the error, and fix the request", e);
	        //LOG.info("HTTP Status: " + e.getStatus());
	        //LOG.info("Error Code: " + e.getErrorCode());
	        //LOG.info("Error Message: " + e.getErrorMessage());
//	    }
	}
	
	public static void pushTargetAlertForDriver(String where,String msg){
		JPushClient jpushClient = new JPushClient(secret, appkey, null, ClientConfig.getInstance());
		PushPayload payload = buildPushObject_all_alias_alert(where,msg);
	    try {
	        PushResult result = jpushClient.sendPush(payload);
	        System.out.println(result);
	        //LOG.info("Got result - " + result);
	    } catch (APIConnectionException e) {
	    	e.printStackTrace();
	    	// Connection error, should retry later
	        //LOG.error("Connection error, should retry later", e);
	    } catch (APIRequestException e) {
	    	e.printStackTrace();
	    	// Should review the error, and fix the request
	        //LOG.error("Should review the error, and fix the request", e);
	        //LOG.info("HTTP Status: " + e.getStatus());
	        //LOG.info("Error Code: " + e.getErrorCode());
	        //LOG.info("Error Message: " + e.getErrorMessage());
	    }
	}
	
	public static void pushTargetAlertForDriverSound(String where,String msg){
		JPushClient jpushClient = new JPushClient(secret, appkey, null, ClientConfig.getInstance());
		PushPayload payload = buildPushObject_all_alias_alertSound(where,msg);
	    try {
	        PushResult result = jpushClient.sendPush(payload);
	        System.out.println(result);
	        //LOG.info("Got result - " + result);
	    } catch (APIConnectionException e) {
	    	e.printStackTrace();
	    	// Connection error, should retry later
	        //LOG.error("Connection error, should retry later", e);
	    } catch (APIRequestException e) {
	    	e.printStackTrace();
	    	// Should review the error, and fix the request
	        //LOG.error("Should review the error, and fix the request", e);
	        //LOG.info("HTTP Status: " + e.getStatus());
	        //LOG.info("Error Code: " + e.getErrorCode());
	        //LOG.info("Error Message: " + e.getErrorMessage());
	    }
	}
	public static void pushTargetForUser(String where,String msg,String aurora_alias){
		JPushClient jpushClient = new JPushClient(secret1, appkey1, null, ClientConfig.getInstance());
	       System.out.println(msg);
	       
	   			try {
	   				if("ios".equals(aurora_alias)){
	   		    		//PushResult result1 = jpushClient.sendIosMessageWithAlias(title, msgContent, alias);
	   		    		PushPayload pp=PushPayload.newBuilder()
	   		    				.setPlatform(Platform.ios())
	   		    				.setMessage(Message.newBuilder()
	   	                        .setMsgContent(msg).build())
	   		    				.setAudience(Audience.alias(where))
//	   		    		.setAudience(Audience.alias(alias))
	   		    		.build();
	   		    		PushResult result = jpushClient.sendPush(pp);
	   			        System.out.println("ios消息="+result);
	   		    	}else{
//	   		    		PushResult result = jpushClient.sendAndroidMessageWithAlias(title, msgContent, alias);
	   		    		PushPayload pp=PushPayload.newBuilder()
	   		    				.setPlatform(Platform.android())
	   		    				.setMessage(Message.newBuilder()
	   	                        .setMsgContent(msg).build())
	   		    				.setAudience(Audience.alias(where))
	   		    		.build();
	   						PushResult result = jpushClient.sendPush(pp);
	   			        System.out.println("Android消息="+result);
	   		    	}
	   			} catch (APIConnectionException e) {
	   				// TODO Auto-generated catch block
	   				e.printStackTrace();
	   			} catch (APIRequestException e) {
	   				// TODO Auto-generated catch block
	   				e.printStackTrace();
	   			}
	}
	public static void pushTargetAlertForUser(String where,String msg,String aurora_alias){
		JPushClient jpushClient = new JPushClient(secret1, appkey1, null, ClientConfig.getInstance());
		PushPayload payload = buildPushObject_all_alias_alert(where,msg);
	    try {
	        PushResult result = jpushClient.sendPush(payload);
	        System.out.println(result);
	        //LOG.info("Got result - " + result);
	    } catch (APIConnectionException e) {
	    	e.printStackTrace();
	    	// Connection error, should retry later
	        //LOG.error("Connection error, should retry later", e);
	    } catch (APIRequestException e) {
	    	e.printStackTrace();
	    	// Should review the error, and fix the request
	        //LOG.error("Should review the error, and fix the request", e);
	        //LOG.info("HTTP Status: " + e.getStatus());
	        //LOG.info("Error Code: " + e.getErrorCode());
	        //LOG.info("Error Message: " + e.getErrorMessage());
	    }
	}
	
	 //消息
	public static void pushTargetMsg(String title,String msgContent,String alias,String aurora_alias){
		JPushClient jpushClient = new JPushClient(secret1, appkey1, null, ClientConfig.getInstance());
		PushPayload payload=PushPayload.newBuilder()
     	.setPlatform(Platform.all()).setAudience(Audience.alias(alias)).setMessage(Message.content(msgContent)).build();
		  System.out.println("消息="+payload);
		  PushResult result1;
		try {
			result1 = jpushClient.sendPush(payload);
			System.out.println(result1);
		} catch (APIConnectionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (APIRequestException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	        
	}
	//快捷地构建推送对象：所有平台，所有设备，内容为 ALERT 的通知
	public static PushPayload buildPushObject_all_all_alert(String msg) {
        return PushPayload.alertAll(msg);
    }
	//所有平台，推送目标是别名为 "alias1"，通知内容为 ALERT
	public static PushPayload buildPushObject_all_alias_alert(String where,String msg) {
		 return PushPayload.newBuilder()
	     	.setPlatform(Platform.all())
	        .setAudience(Audience.alias(where))
	        .setNotification(Notification.alert(msg))
	        .build();
	}
	public static PushPayload buildPushObject_all_alias_alertSound(String where,String msg) {
		Map<String, String> map = new HashMap<>();
		map.put("alert", where);
		map.put("sound", "sound.caf");
		 return PushPayload.newBuilder()
	     	.setPlatform(Platform.all())
	        .setAudience(Audience.alias(where))
	        .setNotification(Notification.ios("ios", map))
	        .build();
	}
	//平台是 Android，目标是 tag 为 "tag1" 的设备，内容是 Android 通知 ALERT，并且标题为 TITLE
	/*public static PushPayload buildPushObject_android_tag_alertWithTitle() {
		return PushPayload.newBuilder()
	    	.setPlatform(Platform.android())
	        .setAudience(Audience.tag("tag1"))
	        .setNotification(Notification.android(ALERT, TITLE, null))
	        .build();
	}*/
	//平台是 iOS，推送目标是 "tag1", "tag_all" 的交集，推送内容同时包括通知与消息 
	//	- 通知信息是 ALERT，角标数字为 5，通知声音为 "happy"，并且附加字段 from = "JPush"；
	//	消息内容是 MSG_CONTENT。通知是 APNs 推送通道的，消息是 JPush 应用内消息通道的。
	//	APNs 的推送环境是“生产”（如果不显式设置的话，Library 会默认指定为开发）
/*	public static PushPayload buildPushObject_ios_tagAnd_alertWithExtrasAndMessage() {
		return PushPayload.newBuilder()
				.setPlatform(Platform.ios())
				.setAudience(Audience.tag_and("tag1", "tag_all"))
				.setNotification(Notification.newBuilder()
						.addPlatformNotification(IosNotification.newBuilder()
								.setAlert(ALERT)
								.setBadge(5)
								.setSound("happy")
						.addExtra("from", "JPush")
						.build())
	        	.build())
				.setMessage(Message.content(MSG_CONTENT))
				.setOptions(Options.newBuilder()
						.setApnsProduction(true)
						.build())
				.build();
	}*/
	//平台是 Andorid 与 iOS，推送目标是 （"tag1" 与 "tag2" 的并集）交（"alias1" 与 "alias2" 的并集），
	//	推送内容是 - 内容为 MSG_CONTENT 的消息，并且附加字段 from = JPush
/*	public static PushPayload buildPushObject_ios_audienceMore_messageWithExtras() {
		return PushPayload.newBuilder()
				.setPlatform(Platform.android_ios())
				.setAudience(Audience.newBuilder()
						.addAudienceTarget(AudienceTarget.tag("tag1", "tag2"))
	                    .addAudienceTarget(AudienceTarget.alias("alias1", "alias2"))
	                    .build())
	            .setMessage(Message.newBuilder()
	                    .setMsgContent(MSG_CONTENT)
	                    .addExtra("from", "JPush")
	                    .build())
	            .build();
	}*/
	
	//推送内容包含SMS信息
	public static void testSendWithSMS() {
		JPushClient jpushClient = new JPushClient(secret, appkey);
	    try {
	    	SMS sms = SMS.content("Test SMS", 10);
	        PushResult result = jpushClient.sendAndroidMessageWithAlias("Test SMS", "test sms", sms, "alias1");
	        //LOG.info("Got result - " + result);
	    } catch (APIConnectionException e) {
	        //LOG.error("Connection error. Should retry later. ", e);
	    } catch (APIRequestException e) {
	        //LOG.error("Error response from JPush server. Should review and fix it. ", e);
	        //LOG.info("HTTP Status: " + e.getStatus());
	        //LOG.info("Error Code: " + e.getErrorCode());
	        //LOG.info("Error Message: " + e.getErrorMessage());
	    }
	}
	/*public static PushPayload buildPushObject_all_all_alert(String ALERT) {
        return PushPayload.alertAll(ALERT);
    }*/
	public static void main(String[] args) {
		pushTargetAlertForDriverSound("driver1518257544579", "dsdsds");
//		pushTargetAlertForUser("user1518180821129","dsdsds","ios");
//		pushTargetForUser("user1518180821129", "dsdsds","ios");
//		pushTargetForDriver("driver1518180677739","dsdsds","ios");
//		pushTargetAlertForDriver("driver1518180677739","dsdsds");
//		buildPushObject_all_alias_alert("16","测试mq");
//		buildPushObject_all_all_alert("测试mq");
	//	pushTarget("16","899999999");
	}
}
